package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {



    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;


    /**
     *  添加菜品
     * 1.接受到数据
     *      添加
     *          添加菜品
     *          根据菜品id添加口味
     *
     * @param dishDto
     */
    @Override
    public void saveWithFlavor(DishDto dishDto) {
        //保存基本的信息到菜品表   1
        this.save(dishDto);
        LambdaQueryWrapper<DishDto> lqw = new LambdaQueryWrapper<>();
        //获取菜品的id
        Long dishId = dishDto.getId();

        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishId);
            dishFlavorService.save(flavor);
        }
    }
    /**
     *
     *  修改
     * 更新菜品信息，同时更新对应的口味信息
     *
     *
     *
     * @param dishDto
     */
    @Override
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表中的基本信息
        this.updateById(dishDto);
        //清理当前菜品对应口味数据---dish_flavor表的delete操作
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(lqw);

        //添加当前提交过来的口味数据---dish_flavor表的insert操作
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.stream().map((item) ->{
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void deleteWithSetmealDish(List<Long> ids) {
        for (Long id : ids) {
            //根据前端返回的id查询数据库
            LambdaQueryWrapper<Dish> dishLqw = new LambdaQueryWrapper<>();
            dishLqw.eq(id != null,Dish::getId,id);
            Dish dish = this.getOne(dishLqw);  //返回dish对象

            //判断dish不为null 并且 状态是起售状态
            if(dish != null && dish.getStatus() == 1){
                throw new CustomException("菜品是出售状态不能删除");
            }

            Long dishId = dish.getId();    //获取dishid用来查与setmealDish表
            LambdaQueryWrapper<SetmealDish> catLqw = new LambdaQueryWrapper<>();
            catLqw.eq(SetmealDish::getDishId,dishId);
            //查询setmealDish表中的dishId   返回集合
            List<SetmealDish> setmealDishList = setmealDishService.list(catLqw);
            if(setmealDishList.size() > 0){
                throw new CustomException("当前菜品已绑定套餐不能删除");
            }
            //根据d ishid获取口味  然后删除口味
            LambdaQueryWrapper<DishFlavor> dishFlavorLqw = new LambdaQueryWrapper<>();
            dishFlavorLqw.eq(DishFlavor::getDishId, dishId);
            dishFlavorService.remove(dishFlavorLqw);
            //删除菜品
            this.removeById(id);
        }
    }

    @Override
    public Page<DishDto> pageWithCateory(Integer page, Integer pageSize, String name) {

        //分页
        Page<Dish> pageInfo = new Page<>(page, pageSize);
        //dishDtoPage集合
        Page<DishDto> dishDtoPage = new Page<>();
        LambdaQueryWrapper<Dish> dishlqw = new LambdaQueryWrapper<>();
        //模糊查询
        dishlqw.like(StringUtils.isNotEmpty(name),Dish::getName,name);
        //根据id升序排序
        dishlqw.orderByAsc(Dish::getPrice);
        //调用方法
        this.page(pageInfo,dishlqw);


        //将pageInfo的数据复制到disDTtoPage里面除 records
        BeanUtils.copyProperties(pageInfo,dishDtoPage,"records");
        //获取pageInfo里records集合存的数据
        List<Dish> dishrecords = pageInfo.getRecords();


        //将dishrecords集合里的数据转到dishDtorecords集合  TODO item=原始对象
        List<DishDto> dishDtorecords = dishrecords.stream().map((item) ->{

            //dishDtorecords集合中存放的DishDto对象   创建DishDto对象
            DishDto dishDto = new DishDto();

            //从item中获取category的id
            Long categoryId = item.getCategoryId();

            //将item中的数据复制给disDto对象
            BeanUtils.copyProperties(item,dishDto);

            //根据id查询   调用方法根据上面已获取到的categoryId 来获取Category对象
            Category dishwithcategory = categoryService.getById(categoryId);
            //判断dishwithcategory
            if(dishwithcategory != null){
                //获取dishwithcategory的名称
                String dishwithcategoryName = dishwithcategory.getName();
                //设置dishDto.CategoryName的属性  将上面获取到category的名称传给
                dishDto.setCategoryName(dishwithcategoryName);
            }
            //给集合返回dishDto对象
            return dishDto;
        }).collect(Collectors.toList());
        dishDtoPage.setRecords(dishDtorecords);
        return dishDtoPage;
    }
}
